package com.dragon.myapplication;

import androidx.lifecycle.ViewModelProvider;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

public class SecondFragment extends Fragment {

    private SecondViewModel mViewModel;
    private ImageView imageView;

    public static SecondFragment newInstance() {
        return new SecondFragment();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view =  inflater.inflate(R.layout.second_fragment, container, false);
        imageView = view.findViewById(R.id.imageView1);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mViewModel = new ViewModelProvider(requireActivity()).get(SecondViewModel.class);
        imageView.setScaleX(mViewModel.scaleFactor);
        imageView.setScaleY(mViewModel.scaleFactor);
        ObjectAnimator objectAnimatorX = ObjectAnimator.ofFloat(imageView,"scaleX",0,0);
        ObjectAnimator objectAnimatorY = ObjectAnimator.ofFloat(imageView,"scaleY",0,0);
        //如果复杂变化可用AnimateSet
//        AnimatorSet animatorSet = new AnimatorSet();
//        animatorSet.playTogether(objectAnimatorX,objectAnimatorY);
        objectAnimatorX.setDuration(500);
        objectAnimatorY.setDuration(500);
        imageView.setOnClickListener(v -> {
            if (! objectAnimatorX.isRunning()) {
                objectAnimatorX.setFloatValues((float) (imageView.getScaleX() + 0.1));
                objectAnimatorY.setFloatValues((float) (imageView.getScaleY() + 0.1));
                mViewModel.scaleFactor += 0.1;
                objectAnimatorX.start();
                objectAnimatorY.start();
            }
        });
    }

}